package com.sfh.dog.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 网络信息管理类
 */
public class NetWorkHelper
{
    
    /**
     * 判断MOBILE网络是否可用
     * 
     * @param context
     * @return
     * @throws Exception
     */
    public static boolean isMobileDataEnable(Context context)
        throws Exception
    {
        if (cm == null)
        {
            cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        }
        boolean isMobileDataEnable = false;
        
        isMobileDataEnable =
            cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        
        return isMobileDataEnable;
    }
    
    private static ConnectivityManager cm;
    
    /**
     * 判断是否有网络连接?
     */
    public static boolean isNetworkAvailable(Context context)
    {
        if (cm == null)
        {
            cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        }
        boolean isConnected = false;
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isAvailable())
        {
            isConnected = activeNetwork.isConnectedOrConnecting();
            LogUtil.i("是否可連接網路 : " + isConnected);
            switch (activeNetwork.getType())
            {
                case ConnectivityManager.TYPE_WIFI:
                    LogUtil.i("目前透過Wifi Network連接");
                    break;
                case ConnectivityManager.TYPE_MOBILE:
                    LogUtil.i("目前透過Mobile Network連接");
                    break;
            }
        }
        else
        {
            LogUtil.i("是否可連接網路 : 不行，没网了");
        }
        
        return isConnected;
    }
    
    /**
     * 判断wifi 是否可用
     * 
     * @param context
     * @return
     * @throws Exception
     */
    public static boolean isWifiDataEnable(Context context)
    {
        if (cm == null)
        {
            cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        }
        boolean isWifiDataEnable = false;
        isWifiDataEnable = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
        return isWifiDataEnable;
    }
    
}
